define(['jquery', '_base'], function ($, _base) {
    var cartName = '_cartData';

    function _setCartData(data) {
        if (typeof data == 'string') {
            //window.localStorage.setItem(cartName, data);
            data = $.parseJSON(data);
            console.log(data);
        }
        data.lastTime = (new Date());
        window.localStorage.setItem(cartName, JSON.stringify(data));
    }

    function _getCartData() {
        if (!!window.localStorage.getItem(cartName)) {
            return $.parseJSON(window.localStorage.getItem(cartName));
        }
        return false;
    }

    // 保存货物  data --{pno:1,price:1}
    function _save(data) {
        var _d = _getCartData();
        var cNumb = data.numb || 1;
        if (!!window.localStorage.getItem(cartName)) {
            if (_hasProduct(data.pno)) {
                _d.total = parseInt(_d.total) + parseInt(cNumb);
                _d.totalPrice = _base.accAdd(parseFloat(_d.totalPrice), _base.accMul(parseFloat(cNumb), parseFloat(data.price)));
                $.each(_d.products, function (i, product) {
                    if (product.pno == data.pno) {
                        _d.products[i].numb = parseInt(_d.products[i].numb) + parseInt(cNumb);
                        _d.products[i].sumPrice = _base.accMul(parseFloat(_d.products[i].numb), parseFloat(_d.products[i].price));
                    }
                });
            } else {
                _d.total = parseInt(_d.total) + parseInt(cNumb);
                _d.totalPrice = _base.accAdd(parseFloat(_d.totalPrice), _base.accMul(parseFloat(cNumb), parseFloat(data.price)));
                _d.products.push({
                    pno: data.pno,
                    numb: cNumb,
                    price: data.price,
                    info: data.info,
                    imgSrc: data.imgSrc,
                    categoryId: data.categoryId,
                    sumPrice: _base.accMul(parseFloat(cNumb), parseFloat(data.price)),
                    isBuy: true
                });
            }
            //_d.lastTime = (new Date());
            _setCartData(_d);
        } else {
            _setCartData({
                total: cNumb,
                totalPrice: _base.accMul(parseFloat(cNumb), parseFloat(data.price)),
                products: [
                    {
                        pno: data.pno,
                        numb: cNumb,
                        price: data.price,
                        info: data.info,
                        imgSrc: data.imgSrc,
                        categoryId: data.categoryId,
                        sumPrice: _base.accMul(parseFloat(cNumb), parseFloat(data.price)),
                        isBuy: true
                    }
                ]
            })
        }
    }

    // 根据产品号查询是否有产品
    function _hasProduct(pno) {
        var products = _getCartData().products;
        var result = false;
        if (!!products && products.length > 0) {
            $.each(products, function (i, product) {
                if (product.pno == pno) {
                    result = true;
                }
            })
        }
        return result;
    }

    // 删除数据
    function _del(pno) {
        if (_hasProduct(pno)) {
            var _d = _getCartData();
            if (_d.products.length > 1) {
                var tPro = [];
                $.each(_d.products, function (i, product) {
                    if (!!product && (product.pno == pno)) {
                        _d.total = parseInt(_d.total) - parseInt(product.numb);
                        _d.totalPrice = _base.accSub(parseFloat(_d.totalPrice), _base.accMul(parseFloat(product.numb), parseFloat(product.price)));
                        _d.products.splice(i, 1);
                    }
                });
                //_d.lastTime = (new Date());
                _setCartData(_d);
            } else {
                _clear();
            }
        } else {
            console.log("不存在产品号" + pno);
            return false;
        }
    }

    // 删除已经购买的商品数据
    function _delBuy() {
        var _d = _getCartData();
        var tPro = [];
        _d.total = 0;
        _d.totalPrice = 0;
        $.each(_d.products, function (i, product) {
            if (!!product && !product.isBuy) {
                _d.total = parseInt(_d.total) + parseInt(product.numb);
                _d.totalPrice = _base.accAdd(parseFloat(_d.totalPrice), _base.accMul(parseFloat(product.numb), parseFloat(product.price)));
                tPro.push(product);
            }
        });
        _d.products = tPro;
        _setCartData(_d);
    }

    // 减少一个产品
    function _reduce(pno) {
        if (_hasProduct(pno)) {
            var _d = _getCartData();
            if (_d.products.length > 1) {
                $.each(_d.products, function (i, product) {
                    if (product.pno == pno) {
                        if (parseInt(product.numb) > 1) {
                            _d.total = parseInt(_d.total) - 1;
                            _d.totalPrice = _base.accSub(parseFloat(_d.totalPrice), _base.accMul(1, parseFloat(product.price)));
                            _d.products[i].numb = parseInt(_d.products[i].numb) - 1;
                            _d.products[i].sumPrice = _base.accMul(parseFloat(_d.products[i].numb), parseFloat(_d.products[i].price));
                        } else {
                            _d.total = parseInt(_d.total) - 1;
                            _d.totalPrice = _base.accSub(parseFloat(_d.totalPrice), _base.accMul(parseFloat(product.numb), parseFloat(product.price)));
                            _d.products.splice(i, 1);
                        }
                    }
                });
                _setCartData(_d);
            } else {
                if (_d.products[0].numb > 1) {
                    _d.total = parseInt(_d.total) - 1;
                    _d.totalPrice = _base.accSub(parseFloat(_d.totalPrice), _base.accMul(1, parseFloat(_d.products[0].price)));
                    _d.products[0].numb = parseInt(_d.products[0].numb) - 1;
                    _d.products[0].sumPrice = _base.accMul(parseFloat(_d.products[0].numb), parseFloat(_d.products[0].price));
                    _setCartData(_d);
                } else {
                    window.localStorage.removeItem(cartName);
                }
            }
        } else {
            console.log("不存在产品号" + pno);
            return false;
        }
    }

    // 获取购物车中商品类型总数
    function _getProductTypeTotal() {
        //if (!!_getCartData()) {
        //    return _getCartData().products.length;
        //} else {
        //    return 0;
        //}
        if (!!_getCartData()) {
            return _getCartData().total;
        } else {
            return 0;
        }

    }

    // 获取购物车中所有商品总数量
    function _getProductsTotal() {
        if (!!_getCartData()) {
            return _getCartData().total;
        } else {
            return 0;
        }
    }

    // 获取购物车中所有商品总价格
    function _getTotalPrice() {
        if (!!_getCartData()) {
            return _getCartData().totalPrice;
        } else {
            return '0.00';
        }
    }

    // 获取指定商品号的商品数量
    function _getnumb(pno) {
        var _d = _getCartData();
        var numb = 0;
        $.each(_d.products, function (i, product) {
            if (product.pno == pno) {
                numb = product.numb;
            }
        });
        return numb;
    }

    // 获取当前产品
    function _getProduct(pno) {
        var _d = _getCartData();
        var thisProduct = {};
        $.each(_d.products, function (i, product) {
            if (product.pno == pno) {
                thisProduct = product;
            }
        });
        return thisProduct;
    }

    // 获取所有可以购买的商品数据
    function _getAllBuy() {
        var _d = _getCartData();
        var allBuy = [];
        if (!!_d && !!_d.products) {
            $.each(_d.products, function (i, product) {
                if (!!product.isBuy) {
                    allBuy.push(product);
                }
            });
        }
        return allBuy;
    }

    // 设置当前产品是否可以购买
    function _setProductBuy(pno, isBuy) {
        var _d = _getCartData();
        if (!!_d && !!_d.products && _d.products.length > 0) {
            $.each(_d.products, function (i, product) {
                if (product.pno == pno) {
                    _d.products[i].isBuy = isBuy;
                }
            });
            _setCartData(_d);
        }
    }

    // 设置所有商品在购买状态
    function _setAllCanBuy() {
        var _d = _getCartData();
        if (!!_d && !!_d.products && _d.products.length > 0) {
            $.each(_d.products, function (i, product) {
                _d.products[i].isBuy = true;
            });
            _setCartData(_d);
        }
    }

    function _isCanBuy() {
        var _d = _getCartData();
        var isBuy = false;
        if (!!_d) {
            if (!!_d.products && _d.products.length > 0) {
                for (var i = 0, len = _d.products.length; i < len; i++) {
                    if (_d.products[i].isBuy) {
                        isBuy = true;
                    }
                }
            }
        }
        return isBuy;
    }

    // 获取购物车最近一次操作时间
    function getLastTime() {
        var _d = _getCartData();
        if (!!_d.lastTime) {
            return _d.lastTime;
        }
        return null;
    }

    // 购物车是否过期  判断规则，离上次最近操作时间间隔超过30min
    // 过期返回真，否则返回假
    function _isOverdue() {
        var overdue = 1 * 30 * 60 * 1000;
        //var overdue = 3000;
        var nowDate = (new Date()).getTime();
        var gDate = getLastTime();
        if (!!gDate) {
            if (Math.abs((nowDate - (new Date(gDate)).getTime())) > overdue) {
                return true;
            }
        }
        return false;
    }

    // 清空购物车
    function _clear() {
        window.localStorage.removeItem(cartName);
    }


    return {
        setData: _setCartData,
        getData: _getCartData,
        hasProduct: _hasProduct,
        save: _save,
        del: _del,
        clear: _clear,
        reduce: _reduce,
        getTotal: _getProductTypeTotal,
        getProductsTotal: _getProductsTotal,
        getTotalPrice: _getTotalPrice,
        getNumb: _getnumb,
        getProduct: _getProduct,
        setProductBuy: _setProductBuy,
        delBuy: _delBuy,
        getAllBuy: _getAllBuy,
        setAllCanBuy: _setAllCanBuy,
        isCanBuy: _isCanBuy,
        isOverdue: _isOverdue
    }
})